<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions 
 * @package    Communityad
 * @copyright  Copyright 2009-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Widgets.php 2011-02-16 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Communityad_Form_Admin_Widgets extends Engine_Form {

  public function init() {
    $view = Zend_Registry::isRegistered('Zend_View') ? Zend_Registry::get('Zend_View') : null;
    $this->setTitle('Create New Ad Block')
            ->setDescription("Create a new Ad Block here. Below, you will be able to choose the page for which you want to create the Ad Block, and the corresponding widget position for it. If an Ad Block already exists for this combination, then the settings for it will be overwritten. For widgetized pages, you will be able to choose whether the Ad Block should be placed automatically. For non-widgetized pages, you will need to place the generated code at the appropriate place in the template file of desired page.");

    // Form: Findout widgetiged form
    $pageTable = Engine_Api::_()->getDbtable('pages', 'core');
    $pageList = $pageTable->fetchAll();
    $pageName = array();
    $displayWidget = array();

    // Find out the 'Page Key' which are selected in drop down, If select any pages from 'Page Dropdown' then '$pagekey' value change acording to the page.
    $pageKey = 0;
    $widgetValue = '';
    $page_id = Zend_Controller_Front::getInstance()->getRequest()->getParam('page_id', null);
    $widget_id = Zend_Controller_Front::getInstance()->getRequest()->getParam('widget_id', null);
    $is_edit = Zend_Controller_Front::getInstance()->getRequest()->getParam('edit', null);
    $is_add_block = 0;

    // Condition: If parameter set then we set that page value in variable.
    if (!empty($page_id)) {
      $pageKey = $page_id;
    } else {
      $pageKey = $pageKey;
    }

    // Make an array of a pages which are available in community, Not consider 'header' & 'footer' page.
    foreach ($pageList as $page) {
      // Condition: Only Page allow which are not 'Header' & 'Footer'.
      if ($page->name != 'header' && $page->name != 'footer') {
        // Condition: Find out the 'First page id'.
        if (empty($pageKey)) {
          $pageKey = $page->page_id;
        }
        // Array: Make an array of 'Admin Pages' which 'key' is 'page id' & 'value' is 'page display name'.
        $pageName[$page->page_id] = $page->displayname;
      }
    }

    // FInd out the PositionType of any page which are not a 'Header' or 'Footer'.
    $page_id = $pageKey;
    // Page_id could be 'none' only in the case of 'none-widgetized page'.
    if ($page_id != 'none') {
      $contentTable = Engine_Api::_()->getDbtable('content', 'core');
      $select = $contentTable->select()
                      ->from($contentTable->info('name'), 'name')
                      ->where('page_id = ?', $page_id);
      $contentValues = $contentTable->fetchAll($select)->toArray();
      $contentArray = array();
      foreach ($contentValues as $key => $name) {
        $contentArray[] = $name['name'];
      }

      // Make an array acording to the page which contain all the widgets which applying for this page.
      if ((!in_array("top", $contentArray)) && (!in_array("bottom", $contentArray)) && in_array("main", $contentArray) && in_array("middle", $contentArray) && (!in_array("left", $contentArray)) && (!in_array("right", $contentArray))) { // For full- width page.
        $displayWidget[7] = "Full Width Ads - Widget 1";
        $displayWidget[8] = "Full Width Ads - Widget 2";
        $displayWidget[30] = "Full Width Sponsored Ads Widget";
        $displayWidget[37] = "Full Width Featured Ads Widget";
      }
      if (in_array("bottom", $contentArray)) { // For full- width page.
        $displayWidget[17] = "Full Width Ads - Widget 1";
        $displayWidget[18] = "Full Width Ads - Widget 2";
        $displayWidget[31] = "Full Width Sponsored Ads Widget";
        $displayWidget[38] = "Full Width Featured Ads Widget";
      }
      if (in_array("top", $contentArray)) { // For full- width page.
        $displayWidget[15] = "Full Width Ads - Widget 1";
        $displayWidget[16] = "Full Width Ads - Widget 2";
        $displayWidget[32] = "Full Width Sponsored Ads Widget";
        $displayWidget[39] = "Full Width Featured Ads Widget";
      }
      if (in_array("left", $contentArray)) { // Left widget
        $displayWidget[1] = "Left Column Ads - Widget 1";
        $displayWidget[2] = "Left Column Ads - Widget 2";
        $displayWidget[33] = "Left Sponsored Ads Widget";
        $displayWidget[40] = "Left Featured Ads Widget";
      }
      if (in_array("right", $contentArray) && in_array("left", $contentArray)) { // Middle widget
        $displayWidget[5] = "Middle Column Ads - Widget 1";
        $displayWidget[6] = "Middle Column Ads - Widget 2";
        $displayWidget[34] = "Middle Sponsored Ads Widget";
        $displayWidget[41] = "Middle Featured Ads Widget";
      }
      if (in_array("right", $contentArray)) { // Right widget
        $displayWidget[3] = "Right Column Ads - Widget 1";
        $displayWidget[4] = "Right Column Ads - Widget 2";
        $displayWidget[35] = "Right Sponsored Ads Widget";
        $displayWidget[42] = "Right Featured Ads Widget";
      }
      if ((in_array("left", $contentArray) && (!in_array("right", $contentArray))) || (in_array("right", $contentArray) && (!in_array("left", $contentArray)))) { // For Extended widget
        $displayWidget[9] = "Extended Right / Left Ads - Widget 1";
        $displayWidget[10] = "Extended Right / Left Ads - Widget 2";
        $displayWidget[36] = "Extended Right / Left Sponsored Ads Widget";
        $displayWidget[43] = "Extended Right / Left Featured Ads Widget";
      }
    } else { // For 'None widgetiged page'
      $displayWidget[21] = "Non-Widgetized Page: Full Width Ads";
      $displayWidget[22] = "Non-Widgetized Page: Middle Column Ads";
      $displayWidget[23] = "Non-Widgetized Page: Left Column Ads";
      $displayWidget[24] = "Non-Widgetized Page: Right Column Ads";
      $displayWidget[25] = "Non-Widgetized Page: Extended Right / Left Ads";
      $displayWidget[26] = "Non-Widgetized Page: Featured Full Width Ads";
      $displayWidget[27] = "Non-Widgetized Page: Featured Middle Column Ads";
      $displayWidget[28] = "Non-Widgetized Page: Featured Left Column Ads";
      $displayWidget[29] = "Non-Widgetized Page: Featured Right Column Ads";
      $displayWidget[44] = "Non-Widgetized Page: Featured Extended Right / Left Ads";
      $displayWidget[45] = "Non-Widgetized Page: Sponsored Full Width Ads";
      $displayWidget[46] = "Non-Widgetized Page: Sponsored Middle Column Ads";
      $displayWidget[47] = "Non-Widgetized Page: Sponsored Left Column Ads";
      $displayWidget[48] = "Non-Widgetized Page: Sponsored Right Column Ads";
      $displayWidget[49] = "Non-Widgetized Page: Sponsored Extended Right / Left Ads";
      $is_add_block = 1;
    }
    $pageName['none'] = "Non-Widgetized Page";

    // Make a description of a 'Site Page' or 'Page Name' which would be change acording to the 'none-widgetized page' or 'widgetized page'.
    if (empty($is_add_block)) {
      $ad_description = 'Select the Ad Widget that you want to be associated to this Ad Block. (Note: You can choose below whether you want this widget to be automatically placed on the page. You will then be able to vertically adjust the position of the widget on this page from the Layout Editor. You are advised not to move the widget horizontally from the Layout Editor.)';
    } else {
      $ad_description = 'Select the Ad Widget that you want to be associated to this Ad Block. To make this Ad Block appear on the desired page(s), please put the code that will be generated at the appropriate location in the respective template file of the page.';
    }

    // Condition: In the case of edit 'Site Page' would be show or in the case of create 'Page Name' would be show.
    if (empty($is_edit)) {
      $this->addElement('Select', 'page_id', array(
          'label' => 'Site Page',
          'description' => $ad_description,
          'multiOptions' => $pageName,
          'onchange' => 'javascript:showAdminPages(this.value, ad_widget_id.value, "page");',
          'value' => $pageKey
      ));
    } else {
      // Condition: For 'Footer Widgets'.
      if ($widget_id != 50) {
        $this->addElement('Select', 'page_id', array(
            'label' => 'Page Name',
            'multiOptions' => $pageName,
            'description' => $ad_description,
            'value' => $pageKey,
            'attribs' => array('disable' => true)
        ));
      }
    }

    //Condition: If 'display widgets array' is not empty or 'widgets id is a 'footer widget''.
    if (!empty($displayWidget) || ($widget_id == 50)) {
      // Condition: Widgets which are already placed that should not be display again.
      if (!empty($pageKey) && empty($is_edit)) {
        // Unset all widgets from widgets array which already placed on manage page.
        $page_displayed_widgets = Engine_Api::_()->getDbtable('pagesettings', 'communityad')->displayPageWidgets($pageKey);
        if (!empty($page_displayed_widgets)) {
          foreach ($page_displayed_widgets as $ad_widgets_id) {
            unset($displayWidget[$ad_widgets_id]);
          }
        }
      }

      // Condition: If Page Id & Widgets Id both are available then that is the case of edit then all option should be prefield.
      if (!empty($is_edit)) {
        // $widgetVAlues = Engine_Api::_()->getDbtable('pagesettings', 'communityad')->adwidgetValue($pageKey, $widget_id);
        $widgetVAlues = Engine_Api::_()->getItem('communityad_pagesetting', $is_edit);
      }
      if (!empty($widgetVAlues)) {
        $ajax_enabled = $widgetVAlues->ajax_enabled;
        $value = $widgetVAlues->value;
        $ad_block_title = $widgetVAlues->ad_block_title;
      } else {
        $ajax_enabled = Engine_Api::_()->getApi('settings', 'core')->getSetting('ad.ajax.based', 0);
        $value = '';
        $ad_block_title = '';
      }
      // Condition: When edit widgets then 'Block Name' should be disabled.
      if (!empty($displayWidget) || ($widget_id == 50)) {
        if ($widget_id != 50) {
          if (empty($is_edit)) {
            $this->addElement('Select', 'ad_widget_id', array(
                'label' => 'Ad Block Widget',
                'description' => 'Select a block name to be placed on the selected page.',
                'multiOptions' => $displayWidget,
                'onchange' => 'javascript:showAdminPages(page_id.value, this.value, "widget");',
                'value' => $widget_id
            ));
          } else {
            $this->addElement('Select', 'ad_widget_id', array(
                'label' => 'Ad Block Widget',
                'description' => 'Select a block name to be placed on the selected page.',
                'multiOptions' => $displayWidget,
                'value' => $widget_id,
                'attribs' => array('disable' => true)
            ));
          }

          $extended_array = array(9, 10, 36, 43, 25, 44, 49);

          if (empty($widget_id)) {
            $id = key($displayWidget);
          } else {
            $id = $widget_id;
          }
          if (in_array($id, $extended_array)) {
            $img_src = 'application/modules/Communityad/externals/images/widget_layout2.gif';
          } else {
            $img_src = 'application/modules/Communityad/externals/images/widget_layout1.gif';
          }

          $preview_link = Zend_Registry::get('Zend_Translate')->_('Click ') . '<a href="javascript:void(0);" onclick="parent.Smoothbox.open(\'' . $img_src . '\');">' . Zend_Registry::get('Zend_Translate')->_('here') . '</a>' . Zend_Registry::get('Zend_Translate')->_(' to view the Position of this Block.');

          $this->addElement('Dummy', 'ad_widget_preview', array(
              'label' => 'Block Position',
              'description' => $preview_link,
          ));
          $this->getElement('ad_widget_preview')->getDecorator('Description')->setOptions(array('placement', 'APPEND', 'escape' => false));
        }

        $this->addElement('Text', 'ad_block_title', array(
            'label' => 'Ad Block Name',
            'description' => 'Enter the name of this ad block. This name is only for your indicative purpose, and will not be displayed to users.',
            'required' => true,
            'allowEmpty' => false,
            'validators' => array(
                array('NotEmpty', true),
            ),
            'value' => $ad_block_title
        ));
        $this->ad_block_title->getValidator('NotEmpty')->setMessage('Please enter a title for the block.', 'isEmpty');

        $this->addElement('Text', 'ad_widget_value', array(
            'label' => 'Ads Limit',
            'description' => 'Enter the number of Ads that you want to display in this Ad Block.',
            'required' => true,
            'allowEmpty' => false,
            'maxlength' => 3,
            'validators' => array(
                array('NotEmpty', true),
            ),
            'value' => $value
        ));
        $this->ad_widget_value->getValidator('NotEmpty')->setMessage('Please enter a value for the block.', 'isEmpty');

        // AD DISPLAY
        $this->addElement('Radio', 'ajax_enabled', array(
            'label' => 'Ajax Based Display',
            'description' => 'Do you want to enable ajax based display of this Ad block ?',
            'multiOptions' => array(
                1 => 'Yes',
                0 => 'No'
            ),
            'value' => $ajax_enabled,
        ));

        if (empty($is_add_block) && $widget_id != 50) { // Show the error msg.
          $this->addElement('Checkbox', 'ad_widget_confirm', array(
              'label' => 'Automatically add this block on the page. (If you select this, then this system will automatically set the widget for this Ad Block on the site page. You will then be able to adjust the vertical position of the ad block widget from the Layout Editor. If you do not select this, then you will need to manually place the ad block widget at the correct position according to the properties of this ad block from the Layout Editor. We recommend you to check this checkbox.)',
              'description' => 'Automatically Add on Page',
              'value' => Engine_Api::_()->getApi('settings', 'core')->getSetting('ad.widget.confirm', 1),
          ));
        }

        $this->addElement('Button', 'submit', array(
            'label' => 'Save Changes',
            'type' => 'submit',
            'ignore' => true
        ));
        if (!empty($is_edit)) {
          $this->setTitle('Edit Ad Block');
          $this->setDescription('Edit this Ad Block.');
        }
      } else {
        $this->addElement('Hidden', 'ad_widget_id', array(
            'value' => 0,
            'order' => 850
        ));
        if ($page_id == 'none') { // Show the error messages.
          $this->addElement('Dummy', 'dummy_msg', array(
              'label' => 'You have already used all the available Ad Blocks for non-widgetized pages. You may view the code to be pasted in the template file of the non-widgetized page from the Manage Ad Blocks page.',
          ));
        } else { // Show the error messages.
          $this->addElement('Dummy', 'dummy_msg', array(
              'label' => 'You have already used all the available Ad Block Widgets for this page. You may manage those ad blocks from the Manage Ad Blocks page, or change the vertical positioning of ad block widgets on this page from the Layout Editor.',
          ));
        }
      }
    }
  }

}